﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CalemShop.Classes;

namespace CalemShop.Forms
{
    public partial class FrmInvoiceExplorer : FrmBaseDock
    {
        private bool changeLoadInvoiceToday = false;

        public FrmInvoiceExplorer()
        {
            InitializeComponent();
        }

        public FrmInvoiceExplorer(FrmCalemShop shop)
            : base(shop)
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            txtMSHD.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtMSHD.AutoCompleteMode = AutoCompleteMode.Suggest;
            txtMSHD.KeyDown += new KeyEventHandler(txtMSHD_KeyDown);
        }


        private void txtMSHD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                int invoiceId = 0;
                int.TryParse(txtMSHD.Text, out invoiceId);
                this.CalemShop.ShowInvoiceDetailForm(invoiceId);
            }
        }

        private void FrmInvoiceExplorer_Load(object sender, EventArgs e)
        {
            txtMSHD.AutoCompleteCustomSource = Database.getInvoiceIds();
            LoadInvoices(true);
        }

        private void LoadInvoices(bool isToday)
        {
            DataTable sr = null;
            if (isToday)
            {
                string today = DateTime.Now.ToString("yyyy-MM-dd");
                sr = Database.getInvoiceIds(today, today);
            }
            else
            {

                string today = DateTime.Now.ToString("yyyy-MM-dd");
                string from = dateTimeFrom.Value.ToString("yyyy-MM-dd");
                string to = dateTimeTo.Value.ToString("yyyy-MM-dd");
                if (string.Compare(from, to, true) == 0 && string.Compare(from, today, true) == 0 && listInvoice.DataSource != null)
                {
                    return;
                }

                sr = Database.getInvoiceIds(from, to);
                changeLoadInvoiceToday = true;
            }
            if (sr != null)
            {
                if (isToday)
                {
                    listInvoiceToday.DataSource = sr;
                    listInvoiceToday.DisplayMember = "invoiceId";
                    listInvoiceToday.ValueMember = "invoiceId";
                    lbDesc.Text = string.Format("Có : {0} hóa đơn", sr.Rows.Count);
                }
                else
                {
                    listInvoice.DataSource = sr;
                    listInvoice.DisplayMember = "invoiceId";
                    listInvoice.ValueMember = "invoiceId";
                }
            }
            else
            {
                if (isToday)
                {
                    listInvoiceToday.DataSource = null;
                    lbDesc.Text = string.Empty;
                }
                else
                {
                    listInvoice.DataSource = null;
                }
            }
        }

        private void dateTime_ValueChanged(object sender, EventArgs e)
        {
            LoadInvoices(false);
        }

        private void listInvoice_DoubleClick(object sender, EventArgs e)
        {
            ListBox control = sender as ListBox;
            if (control.SelectedItem != null)
            {
                txtMSHD.Text = control.Text;
                int invoiceId = 0;
                int.TryParse(control.Text, out invoiceId);
                this.CalemShop.ShowInvoiceDetailForm(invoiceId);
            }
        }

        public void AddNewInvoiceId(long invoiceID)
        {
            try
            {
                if (tabControl.SelectedTab != tabToday && changeLoadInvoiceToday)
                {
                    tabControl.SelectedTab = tabToday;
                }

                DataTable sr = (DataTable)listInvoiceToday.DataSource;
                if (sr != null)
                {
                    DataRow r = sr.NewRow();
                    r["invoiceId"] = invoiceID;
                    sr.Rows.Add(r);
                }
            }
            catch (Exception ee)
            {
                Reporter.WriteErrorEx(ee);
            }
        }

        private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl.SelectedTab == tabToday && changeLoadInvoiceToday)
            {
                LoadInvoices(true);
                changeLoadInvoiceToday = false;
            }
            else if (tabControl.SelectedTab == tabHistory)
            {
                LoadInvoices(false);
            }
        }

        private void listInvoice_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Handled == false && e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                ListBox control = sender as ListBox;
                if (control.SelectedItem != null)
                {
                    txtMSHD.Text = control.Text;
                    int invoiceId = 0;
                    int.TryParse(control.Text, out invoiceId);
                    this.CalemShop.ShowInvoiceDetailForm(invoiceId);
                }

                return;
            }
        }
    }
}
